package com.defangit.award.extend.repeat_by_share.orm;

import com.defangit.award.extend.repeat_by_share.domain.Spread;
import laogen.hibernate.CommonFinder;
import laogen.hibernate.CommonService;
import laogen.util.StringUtil;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 * Created by zhangfagen on 2019/1/4.
 */
public class SpreadService extends CommonService<Spread>{

    public SpreadFinder findAll(){
        return new SpreadFinder();
    }
    public class SpreadFinder extends CommonFinder<Spread>{
        DetachedCriteria criteria=DetachedCriteria.forClass(Spread.class);
        @Override
        public Criteria getCriteria() {
            return criteria.getExecutableCriteria(getSession());
        }

        @Override
        public DetachedCriteria getDetachedCriteria() {
            return criteria;
        }

        public SpreadFinder user(String openid){
            if(StringUtil.notEmpty(openid)){
                criteria=criteria.add(Restrictions.eq("openId",openid));
            }
            return this;
        }
        public SpreadFinder invited(String openid){
            if(StringUtil.notEmpty(openid)){
                criteria=criteria.add(Restrictions.eq("invitedOpenId",openid));
            }
            return this;
        }
        public SpreadFinder award(Integer id){
            if(id!=null && id!=0){
                criteria=criteria.add(Restrictions.eq("award.id",id));
            }
            return this;
        }
    }
}
